<template>
    <div>
      <my-header></my-header>
      <el-container class="login_header">
        <el-main class="login_main">
            <el-form :model="ruleForm2" status-icon :rules="rules2" ref="ruleForm2" label-width="100px" class="demo-ruleForm">
                <el-form-item  prop="number" class="Form_items">
                    <el-input v-model="ruleForm2.number" type="text" placeholder="身份证号码"></el-input>
                </el-form-item>
                
                <el-form-item  prop="username" class="Form_items">
                    <el-input v-model="ruleForm2.username" placeholder="姓名"></el-input>
                </el-form-item>
               
                <el-form-item  prop="pass" class="Form_items">
                    <el-input type="password" v-model="ruleForm2.pass" autocomplete="off" placeholder="请输密码"></el-input>
                </el-form-item>
                <el-form-item  prop="checkPass" class="Form_items">
                    <el-input type="password" v-model="ruleForm2.checkPass" autocomplete="off" placeholder="确认密码"></el-input>
                </el-form-item>
                <el-form-item class="Form_items">
                    <el-button type="primary" @click="submitForm('ruleForm2')" class="Sub">提交</el-button>
                </el-form-item>
            </el-form>
        </el-main>
    </el-container>
    </div>
</template>

<script>
  import Header from "./header"
    export default {
      components:{
        myHeader:Header
      },
        data() {
      var checkName = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('姓名不能为空'));
        }else{
            callback();
        }
      };
      var checkNum = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('身份证号码不能为空'));
        }else{
            callback();
        }
        };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm2.checkPass !== '') {
            this.$refs.ruleForm2.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm2.pass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        ruleForm2: {
          pass: '',
          checkPass: '',
          username: '',
          number:""
        },
        rules2: {
            number:[
                {validator: checkNum, trigger: 'blur'}
            ],
          pass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ],
          username: [
            { validator: checkName, trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
              this.$confirm('请确认您输入的是真实的身份证和姓名，否则将导致无法查询', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
         let formData=new FormData()
            formData.append("personNumber",this.ruleForm2.number)
            formData.append("userName",this.ruleForm2.username)
            formData.append("password",this.ruleForm2.pass)
            let url="/user/register"
            this.axios.post(url,formData).then(res=>{
                this.ruleForm2.number=""
                this.ruleForm2.username=""
                this.ruleForm2.pass=""
                this.ruleForm2.checkPass=""
                if(res.data.status==0){
                    this.$alert('', '注册成功！是否现在登录', {
                            confirmButtonText: '登录',
                            callback: action => {
                                // window.location.hash="/login"
                                this.$router.push("/login")
                            }
                            });
                }else{
                    this.$message(res.data.msg)
                }
            },err=>{
                this.$message(err)
            })
        })
              
            
            
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
    }
</script>

<style scoped>
  .login_header{
        width: 30%;
        margin: 100px auto 
    }
    .login_main{
        border: 1px solid #ebebeb;
    }
    .Sub{
        width: 100%
    }
    .Form_items >>> .el-form-item__content{
        margin-left: 0px !important;
    }
</style>